import os,time,base64
from PIL import Image,ImageFont,ImageDraw
from io import BytesIO

class PICTURE:
    def __init__(self, fonttype=None, size=20):
        if(not fonttype):
            self.fonttype = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fonts", "zpix.ttf")
        else:
            self.fonttype = fonttype
        self.fontSize = size
        self.font = ImageFont.truetype(self.fonttype,size=size)
    
    def getFontHeight(self):
        return self.fontSize
    
    def getTextWidth(self, text):
        return self.font.getlength(text)
    
    def createCanvas(self, width, height, color="white"):
        self.im = Image.new("RGBA", (width, height), color)
        self.dr = ImageDraw.Draw(self.im)
                    
    def drawRectangle(self, x, y, width, height, *, fill=None, outline=None, boardwidth=1):
        if(self.dr):
            self.dr.rectangle([(x, y),(x + width, y + height)], fill=fill, outline=outline, width=boardwidth)
        return x + width, y + height
    
    def drawText(self, x, y, text, *, color, fontsize=0):
        if(fontsize > 0 and fontsize != self.fontSize):
            font = ImageFont.truetype(self.fonttype, size=fontsize)
        else:
            font = self.font
        if(self.dr):
            self.dr.text((x, y), text, font=font, fill=color)
            w, h = self.font.getsize(text)
        return x + w, y + h
    
    def drawImage(self, res, scale, x, y):
        img = Image.open(res).convert("RGBA")
        width = int(img.size[0] * scale)
        height = int(img.size[1] * scale)
        out = img.resize((width, height))
        r,g,b,a = out.split()
        self.im.paste(out, (x, y), mask=a)
        out.close()
        img.close()

    def calcImageSize(self, res, scale):
        img = Image.open(res).convert("RGBA")
        width = int(img.size[0] * scale)
        height = int(img.size[1] * scale)
        img.close()
        return width, height

    def drawLine(self, x, y, ex, ey, *, color="black", width=0):
        if(self.dr):self.dr.line([x, y, ex, ey], fill=color, width=width)

    def drawEllipse(self, x, y, r, *, width=1, outline="black", fill="white"):
        offset = width/2.0
        left = x - r - offset
        top = y - r - offset
        right = x + r + offset
        bottom = y + r + offset
        self.dr.ellipse([left, top, right, bottom], outline=outline, fill=fill, width=width)
        return  left + (right - left)//2, top + (bottom - top)//2

    def calcEllipsePoint(self, x, y, r, *, width=1):
        offset = width/2.0
        left = x - r - offset
        top = y - r - offset
        right = x + r + offset
        bottom = y + r + offset
        return  left + (right - left)//2, top + (bottom - top)//2        
        
    def show(self, title=None):
        if(self.im):self.im.show(title=title)
    
    def getHttpImg(self):
        return self.getBase64()
    
    def getBase64(self):
        imgIO = BytesIO()
        self.im.save(imgIO, 'png')
        return base64.b64encode(imgIO.getvalue()).decode("utf-8")
    
    def save(self, filename, savedir):
        if(not savedir):savedir = os.path.join(os.path.dirname(os.path.realpath(__file__)))
        if(not os.path.exists(savedir)):os.makedirs(savedir)
        if(not filename):filename = time.strftime("%Y-%m-%d_%H_%M_%S",time.localtime())
        self.im.save(os.path.join(savedir, filename + ".png"))
    